package yxy.game.pm2.bean.module;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.joda.time.DateTime;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.bean.order.OrderInfo;

public class OrderInfoModule {

	public static final int orderinforuning = 0;		//订单待处理
	public static final int orderinfofinish = 1;		//订单处理完毕
	public static final int orderinfofail = 2;			//订单失败

	// fun===========================================================================

	/**
	 *
	 * @param goodsid
	 * @param channel
	 * @param myorderid
	 * @param ordercreatetime
	 * @param orderstate
	 * @param platorderid
	 * @param acttype
	 * @param playercode
	 * @param curs
	 * @param serverid
	 * @param upDateTime
	 * @param addvipexp
	 * @return
	 */
	static public OrderInfo createOrderInfo(int goodsid, String channel, String myorderid, DateTime ordercreatetime,
			int orderstate, String platorderid, int acttype, String playercode, ArrayList<Currency> curs,
			int serverid, DateTime upDateTime, int addvipexp, int payamount, int goodsprice) {
		OrderInfo o = new OrderInfo();
		o.setGoodsid(goodsid);
		o.setChannel(channel);
		o.setMyorderid(myorderid);
		o.setOrdercreatetime(ordercreatetime);
		o.setOrderstate(orderstate);
		o.setPlatorderid(platorderid);
		o.setActtype(acttype);
		o.setGoodsprice(goodsprice);
		o.setPayamount(payamount);
		o.setPlayercode(playercode);

		if(curs == null || curs.size() == 0) {
			o.setCurs(null);
		}else {
			o.setCurs(curs);
		}

		o.setServerid(serverid);
		o.setUpDateTime(upDateTime);
		o.setAddvipexp(addvipexp);
		return o;
	}

	/**
	 *
	 * @param goodsid
	 * @param channel
	 * @param myorderid
	 * @param ordercreatetime
	 * @param platorderid
	 * @param acttype
	 * @param playercode
	 * @param curs
	 * @param serverid
	 * @param upDateTime
	 * @param addvipexp
	 * @return
	 */
	static public OrderInfo createOrderInfo(int goodsid, String channel, String myorderid, DateTime ordercreatetime,
			String platorderid, int acttype, String playercode, ArrayList<Currency> curs, int serverid,
			DateTime upDateTime, int addvipexp, int payamount, int goodsprice) {

		return createOrderInfo(goodsid, channel, myorderid, ordercreatetime, orderinforuning, platorderid, acttype,
				playercode, curs, serverid, upDateTime, addvipexp, payamount, goodsprice);
	}

	/**			//TODO:登陆时返回
	 * 获取未完成的订单id
	 * @param pcode
	 * @return
	 */
	static public JSONArray getRunningOrderInfoId(String pcode) {
		try {
			JSONArray rarr = new JSONArray();

			Map<String, OrderInfo> oimap = new HashMap<String, OrderInfo>();
			oimap  = OrderInfo.all(pcode);

			for(String myorderid:oimap.keySet()) {
				OrderInfo oi = oimap.get(myorderid);
				if(oi.getOrderstate() == orderinforuning) {
					rarr.add(myorderid);
				}
			}

			return rarr;
		} catch (Exception e) {
			e.printStackTrace();
			return new JSONArray();
		}
	}


	/**
	 *
	 * @param curs
	 * @return
	 */
	static public JSONArray getArrForCur(List<Currency> curs) {
		JSONArray rarr = new JSONArray();
		for(int i = 0; i < curs.size(); i++) {
			JSONObject json = new JSONObject();
			Currency cur = curs.get(i);
			json.put("type", cur.getType().getNumber());
			json.put("dataId", cur.getDataId());
			json.put("number", cur.getNumber());
			rarr.add(json);
		}
		return rarr;
	}

	static public void dealFinishOrderInfo(OrderInfo oi) {
		oi.setOrderstate(orderinfofinish);
		OrderInfo.set(oi.getPlayercode(), oi);
	}

}
